var countli=0;
var ulwidth=0;
var ultop=0;
var mintop=0;
var maxtop=0;
var htmlstr="";
var listes=new Array(20);
var listTopes=new Array(20);

var listpices=new Array();
var picnum=0;
	
$(function(){
    var i=0;
    $(window).bind("scroll",function(event){
		doc_get();
    });
});

$(document).ready(doc_ready);

/*****************/
$(window).resize(function(){
  var winwidth=$(window).width();
	countli=(winwidth-40)/230;
	if(countli >=0){
           countli = Math.floor(countli);
       }else{
           countli = Math.ceil(countli);
       }
	
    ulwidth=countli*230-10;
    ultop=0;
    mintop=10;
    for(var i=0;i<listTopes.length;i++){
	   listTopes[i]=10;
	}
	$("#imglist").css("width",ulwidth);
	$("#imglist").css("visibility","visible");
	$("#imglist li").each(function(){
		mintop=listTopes[0];
            maxtop=listTopes[0];
            var tempmin=0;
            var tempmax=0;
            for(var j=0;j<countli;j++){
                if(listTopes[j]<mintop){
                   mintop=listTopes[j];
                   tempmin=j;
                }
            }
            
            var left=230*tempmin;
            $(this).css("left",left+"px");
            $(this).css("top",listTopes[tempmin]+"px");
		
		listTopes[tempmin]=listTopes[tempmin]+(parseInt($(this).find('img').attr('height')))+88+10;
            
            for(var j=0;j<countli;j++){
                if(listTopes[j]>maxtop){
                    maxtop=listTopes[j];
                }
            }
		
	});
	ultop=maxtop;
	$("#imglist").css("height",ultop);
	
	doc_get();
	
});

/*****************/
function doc_get(){
	var doc_height = $(document).height();
		var scroll_top = $(document).scrollTop();
		var window_height = $(window).height();
		
        if(scroll_top+window_height>=doc_height){
            //
            htmlstr=$("#imglist").html();
			
				$.post("getjson.php",{maxnum:countli*3,minnum:0,action:1},function(result){
					var jsvar=jQuery.parseJSON(result);
					$.each( jsvar, function (i, o) {
			            mintop=listTopes[0];
			            maxtop=listTopes[0];
			            var tempmin=0;
			            var tempmax=0;
			            for(var j=0;j<countli;j++){
			                if(listTopes[j]<mintop){
			                   mintop=listTopes[j];
			                   tempmin=j;
			                }
			            }
			            
			            var left=230*tempmin;
			            
			          
			           htmlstr=htmlstr+"<li style='left: "+left+"px; top: "+listTopes[tempmin]+"px;'><div class='imgbox'><a title='"+o.picname+"'"
			           +" target='_blank' href='#'><img width='200' height='"+(parseInt(o.picheigh))*200/(parseInt(o.picwidth))
			           +"' title='"+o.picname+"' alt='"+o.picname+"' src='../imges/"+o.picsrc+".jpg'></a></div><div class='titlebox'>"
			           +"<a title='"+o.picname+"' href='#'>"+o.picname+"</a></div><div class='attr clearfix'><p><a target='_blank' href='#'>"
			           +o.picmark+"</a></p></div></li>";
			           
			            listTopes[tempmin]=listTopes[tempmin]+(parseInt(o.picheigh))*200/(parseInt(o.picwidth))+88+10;
			            
			            for(var j=0;j<countli;j++){
			                if(listTopes[j]>maxtop){
			                    maxtop=listTopes[j];
			                }
			            }
			            
					});
			        ultop=maxtop;
			        $("#imglist").css("height",ultop);
			        $("#imglist").html(htmlstr);
				});
			
			
        }
}
function doc_ready(){
	var winwidth=$(window).width();
	countli=(winwidth-40)/230;
	if(countli >=0){
           countli = Math.floor(countli);
       }else{
           countli = Math.ceil(countli);
       }
	
    ulwidth=countli*230-10;
    ultop=0;
    mintop=10;
	
	
	for(var i=0;i<listTopes.length;i++){
	   listTopes[i]=10;
	}
	
	
	$.post("getjson.php",{maxnum:countli*3,minnum:0,action:0},function(result){
		var jsvar=jQuery.parseJSON(result);
		
		$("#imglist").css("width",ulwidth);
		$("#imglist").css("visibility","visible");
		$.each( jsvar, function (i, o) {
            
            mintop=listTopes[0];
            maxtop=listTopes[0];
            var tempmin=0;
            var tempmax=0;
            for(var j=0;j<countli;j++){
                if(listTopes[j]<mintop){
                   mintop=listTopes[j];
                   tempmin=j;
                }
            }
            
            var left=230*tempmin;
            
           htmlstr=htmlstr+"<li style='left: "+left+"px; top: "+listTopes[tempmin]+"px;'><div class='imgbox'><a title='"+o.picname+"'"
           +" target='_blank' href='#'><img width='200' height='"+(parseInt(o.picheigh))*200/(parseInt(o.picwidth))
           +"' title='"+o.picname+"' alt='"+o.picname+"' src='../imges/"+o.picsrc+".jpg'></a></div><div class='titlebox'>"
           +"<a title='"+o.picname+"' href='#'>"+o.picname+"</a></div><div class='attr clearfix'><p><a target='_blank' href='#'>"
           +o.picmark+"</a></p></div></li>";
           
            listTopes[tempmin]=listTopes[tempmin]+(parseInt(o.picheigh))*200/(parseInt(o.picwidth))+88+10;
            
            for(var j=0;j<countli;j++){
                if(listTopes[j]>maxtop){
                    maxtop=listTopes[j];
                }
            }
            
		});
        ultop=maxtop;
        $("#imglist").css("height",ultop);
        $("#imglist").html(htmlstr);
	});
}